package admin.handler;

import admin.common.LoginContext;
import com.baomidou.mybatisplus.core.handlers.MetaObjectHandler;
import org.apache.ibatis.reflection.MetaObject;
import org.springframework.stereotype.Component;

import java.time.LocalDateTime;

/**
 * @author xf07b1
 * @wechat xf07b1
 * @description 公共字段自动填充
 */
@Component
public class MybatisMetaHandler implements MetaObjectHandler {
    /**
     * 插入自动填充
     */
    @Override
    public void insertFill(MetaObject metaObject) {
        metaObject.setValue("createTime", LocalDateTime.now());
        metaObject.setValue("updateTime", LocalDateTime.now());

        metaObject.setValue("createBy", LoginContext.get() == null ? null : LoginContext.get().getId());
        metaObject.setValue("updateBy", LoginContext.get() == null ? null : LoginContext.get().getId());
    }

    /**
     * 修改自动填充
     */
    @Override
    public void updateFill(MetaObject metaObject) {
        metaObject.setValue("updateTime", LocalDateTime.now());
        metaObject.setValue("updateBy", LoginContext.get().getId());

    }

}
